package cn.elead.chaos.sso.server.web.system.controller;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.elead.chaos.framework.properties.ChaosProperties;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.sso.core.user.LoginUser;
import cn.elead.chaos.sso.core.util.SsoUtils;
import cn.elead.chaos.sso.server.web.login.service.RedisSaveService;
import cn.elead.chaos.sso.server.web.login.service.StringRedisService;
import cn.elead.chaos.sso.server.web.monitor.domain.SysLogininfor;
import cn.elead.chaos.sso.server.web.monitor.service.ISysLogininforService;
import cn.elead.chaos.sso.server.web.system.domain.SysMenu;
import cn.elead.chaos.sso.server.web.system.domain.SysUser;
import cn.elead.chaos.sso.server.web.system.service.ISsoIndexService;
import cn.elead.chaos.sso.server.web.system.service.ISysMenuService;
import cn.elead.chaos.sso.server.web.system.service.ISysUserService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 首页 业务处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping
public class SysIndexController extends BaseController {

	@Autowired
	private ISysMenuService menuService;

	@Autowired
	private ISysUserService userService;

	@Autowired
	private ChaosProperties chaos;
	
	@Autowired
	private ISsoIndexService ssoIndexService;
	
	@Autowired
	private StringRedisService<String, LoginUser> userRedisService;
	
	@Autowired
	private ISysLogininforService logininforService;


	// 系统首页
	@GetMapping("/system/index")
	public String index(ModelMap mmap) {
		// 取身份信息
		SysUser user = userService.selectUserById(SsoUtils.getUserId());
		// 根据用户id取出菜单
		List<SysMenu> menus = menuService.selectMenusByUser(user);
		mmap.put("menus", menus);
		mmap.put("user", user);
		mmap.put("copyrightYear", chaos.getCopyrightYear());
		mmap.put("demoEnabled", chaos.getDemo().isEnabled());
		return "index";
	}

	// 切换主题
	@GetMapping("/system/switchSkin")
	public String switchSkin(ModelMap mmap) {
		return "skin";
	}

	// 系统介绍
	@GetMapping("/system/main")
	public String main(ModelMap mmap) {
		mmap.put("version", chaos.getVersion());
		//接入系统数
		mmap.put("clientCount", ssoIndexService.selectClientCount());
		//今日登录人数
		mmap.put("loginCount", ssoIndexService.selectlLoginCount());
		//在线人数
		Set<String> sessions = userRedisService.keys(RedisSaveService.CACHE_USER + "*");
		if (ObjectUtil.isNotNull(sessions)) {
			sessions = sessions.stream().map(a -> StrUtil.replace(a, RedisSaveService.CACHE_USER, StrUtil.EMPTY)).collect(Collectors.toSet());
		}
		SysLogininfor userOnline = new SysLogininfor();
		userOnline.setSessions(sessions);
		List<SysLogininfor> list = logininforService.selectLogininBySessions(userOnline);
		mmap.put("onlineCount", list.size());
		//在职员工
		mmap.put("employCount", ssoIndexService.selectEmployCount());
		
		return "main";
	}
}
